/********************************************************************************
 * BackGroundAction.java
 *
 * Created by Jigar Mistry
 * Last modified Date: Dec 31, 2008 12:11:23 PM by Jigar Mistry
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/
package com.printeverywhere.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.printeverywhere.delegator.AuctionListingFeesServiceDelegator;
import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.dto.AuctionListingFees;
import com.printeverywhere.dto.BackgroundProcess;

public class BackGroundAction extends AdminSystemSetup {
	/** Logger for this class. */
	private static final Logger logger = Logger.getLogger(BackGroundAction.class);

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	private List<BackgroundProcess> backgroundProcessList = new ArrayList<BackgroundProcess>();

	private Map<Integer, BackgroundProcess> backgroundProcessMap;

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@SuppressWarnings("unchecked")
	public String execute() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}
		try{
			getBackgroundData();
		}catch (Exception e) {
			return INPUT;
		}
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	public String updateBackgroundProcess() throws Exception{
		try{
			for (BackgroundProcess backgroundProcess : this.backgroundProcessMap.values()) {
				if(backgroundProcess.getUpdateTime() != null){
					CommonFunctionDelegator.generalUpdatebyobj(backgroundProcess);
				}else{
					addActionError("Enter proper value for update time.");
					return execute();
				}
			}
		} catch (Exception e) {
			addActionError("Error while updating Background Process");
			return execute();
		}

		return SUCCESS;
	}
	@Override
	public void validate() {
		super.validate();
		try{
			Pattern patternForNo = Pattern.compile("^[0-9]*");
			Matcher fit = null;
			for (BackgroundProcess backgroundProcess : this.backgroundProcessMap.values()) {
				if(backgroundProcess.getUpdateTime() != null){
					fit = patternForNo.matcher(backgroundProcess.getUpdateTime().toString());
					if(!fit.matches()){
						addActionError("Please, enter proper value for update time.");
					}
				}
			}
		}catch (Exception e) {
			addActionError("Error while validating background data.");
		}finally{
			if(super.hasFieldErrors()){
				getBackgroundData();
			}
		}

	}
	public void getBackgroundData(){
		try{
			this.backgroundProcessMap = new HashMap<Integer, BackgroundProcess>();
			this.backgroundProcessList = (List<BackgroundProcess>) CommonFunctionDelegator.getAllfromtable(BackgroundProcess.class.getName());
			for (BackgroundProcess backgroundProcess : this.backgroundProcessList) {
				this.backgroundProcessMap.put(backgroundProcess.getId(), backgroundProcess);
			}
		}catch (Exception e) {
			addActionError("Problem in displaying background processes data.");
		}
	}
	public Map<Integer, BackgroundProcess> getBackgroundProcessMap() {
		return backgroundProcessMap;
	}

	public void setBackgroundProcessMap(
			Map<Integer, BackgroundProcess> backgroundProcessMap) {
		this.backgroundProcessMap = backgroundProcessMap;
	}

	public List<BackgroundProcess> getBackgroundProcessList() {
		return backgroundProcessList;
	}
}

